package pres.moling.common.tool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * Local系列日期类工具
 */
public interface LocalDateHelper {
    /**
     * 将指定的 LocalDateTime 类型输出为日期字符串
     */
    static String dataFormat(LocalDateTime dateTime) {
        return dateFormat(dateTime.toLocalDate());
    }

    /**
     *
     */
    static String dateFormat(LocalDate date) {
        return date.format(DateTimeFormatter.ISO_DATE);
    }

    /**
     * 转换时间字符串
     */
    static String timeFormat(LocalTime time) {
        return time.format(DateTimeFormatter.ISO_TIME);
    }

    static String timeFormat(LocalDateTime dateTime) {
        return timeFormat(dateTime.toLocalTime());
    }

    /**
     * 字符串转日期
     */
    static LocalDate parseDate(CharSequence date) {
        return LocalDate.parse(date);   // Obtains an instance of } from a text string such as } .
    }

    static LocalDate parseDate(CharSequence date, DateTimeFormatter formatter) {
        return LocalDate.parse(date, formatter);   // Obtains an instance of } from a text string using a specific formatter.
    }

    static LocalDateTime parseDateTime(CharSequence dateTime) {
        return LocalDateTime.parse(dateTime);   // Obtains an instance of } from a text string such as } .
    }

    static LocalDateTime parseDateTime(CharSequence dateTime, DateTimeFormatter formatter) {
        return LocalDateTime.parse(dateTime, formatter);   // Obtains an instance of } from a text string using a specific formatter.
    }

    static String format(LocalDate date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));   // Creates a formatter using the specified pattern.
    }

    static String format(LocalDateTime date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));   // Creates a formatter using the specified pattern.
    }

    static String format(LocalTime date, String format) {
        return date.format(DateTimeFormatter.ofPattern(format));   // Creates a formatter using the specified pattern.
    }

    static LocalTime parseLocalTime(String dateStr) {
        return LocalTime.parse(dateStr, dateFormat(dateStr));   // Obtains an instance of } from a text string using a specific formatter.
    }

    static DateTimeFormatter dateFormat(String dateStr) {
        return switch (dateStr.trim().length()) {   // Returns the length of this string. The length is equal to the number of Unicode
            case 8 -> DateTimeFormatter.ofPattern("yyyyMMdd");   // Creates a formatter using the specified pattern.
            case 10 -> DateTimeFormatter.ofPattern("yyyy-MM-dd");   // Creates a formatter using the specified pattern.
            case 19 ->
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");   // Creates a formatter using the specified pattern.
            case 23 ->
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss,SSS");   // Creates a formatter using the specified pattern.
            case 28 ->
                    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");   // Creates a formatter using the specified pattern.
            default ->
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");   // Creates a formatter using the specified pattern.
        };
    }

    static LocalDate parseLocalDate(String dateStr) {
        return LocalDate.parse(dateStr, dateFormat(dateStr));   // Obtains an instance of } from a text string using a specific formatter.
    }


    static LocalDateTime parseLocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, dateFormat(dateStr));   // Obtains an instance of } from a text string using a specific formatter.
    }

    /**
     * LocalDateTime 转 Date
     */
    static Date coverDate(LocalDateTime dateTime) {
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());   // Converts this date-time to an } .
    }

    /**
     * Date 转 LocalDateTime
     */
    static LocalDateTime coverLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();   // Gets the } part of this date-time.
    }

}
